#include <sourcemod>
#include <sdktools>

/**************************************************************
VARIABLES
**************************************************************/
new g_FireOrange;
new g_HaloSprite;
new g_ExplosionSprite;

/**************************************************************
PLUGIN INFORMATION
**************************************************************/
public Plugin:myinfo =
{
	name = "Boom Gun",
	author = "Eakgnarok",
	description = "When you fire a specific place with a xm1014, Boom happenned!",
	version = "1.0.0",
	url = "http://eakgnarok.pe.kr"
};

/**************************************************************
HEADER FUNCTIONS
**************************************************************/
public OnPluginStart()
{
	g_FireOrange = PrecacheModel("materials/sprites/fire2.vmt");
	g_HaloSprite = PrecacheModel("materials/sprites/halo01.vmt");
	g_ExplosionSprite = PrecacheModel("materials/sprites/sprite_fire01.vmt");
	PrecacheSound("ambient/explosions/explode_8.wav", true);
	
	HookEvent("weapon_fire", Event_OnWeaponFire);
}

public Action:Event_OnWeaponFire(Handle:event, const String:name[], bool:dontBroadcast)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid"));
	static String:g_SWeapon[32];
	
	GetEventString(event, "weapon", g_SWeapon, sizeof(g_SWeapon));
	
	if (StrEqual(g_SWeapon, "xm1014")) // 구체화 작업이 필요하다. (참고 - Shot Between Reloading, Double Shot Check, Ammo Check)
	{
		Boom(client);
	}
}

/**************************************************************
GENERAL FUNCTIONS
**************************************************************/
public Action:Boom(client)
{
	new g_Explosion = CreateEntityByName("env_explosion");
	new g_HaloColor[4] = {188, 220, 255, 200};
	new Float:EyeVec[3];
	
	GetEyePosition(client, EyeVec);
	
	DispatchKeyValueVector(g_Explosion, "Origin", EyeVec);
	DispatchKeyValue(g_Explosion,"iMagnitude", "100");
	DispatchKeyValue(g_Explosion,"iRadiusOverride", "0");
  	DispatchSpawn(g_Explosion);
	AcceptEntityInput(g_Explosion, "Explode");
	AcceptEntityInput(g_Explosion, "Kill");
	
	EmitSoundToAll("ambient/explosions/explode_8.wav", SOUND_FROM_WORLD, SNDCHAN_AUTO, SNDLEVEL_NORMAL, SND_NOFLAGS, SNDVOL_NORMAL, SNDPITCH_NORMAL, -1, EyeVec, NULL_VECTOR, true, 0.0);
	
	TE_SetupExplosion(EyeVec, g_ExplosionSprite, 10.0, 1, 0, 600, 5000);
	TE_SendToAll();
	TE_SetupBeamRingPoint(EyeVec, 10.0, 500.0, g_FireOrange, g_HaloSprite, 0, 10, 0.6, 10.0, 0.5, g_HaloColor, 10, 0);
  	TE_SendToAll();
}

/**************************************************************
STOCK FUNCTIONS
**************************************************************/
public GetEyePosition(client, Float:array[3])
{
	new Float:start[3], Float:end[3], Float:angle[3];
	
	GetClientEyePosition(client, start);
	GetClientEyeAngles(client, angle);
	
	GetAngleVectors(angle, end, NULL_VECTOR, NULL_VECTOR);
	
	NormalizeVector(end, end);
	
	TR_TraceRayFilter(start, angle, MASK_PLAYERSOLID, RayType_Infinite, GEP_TEF, client);
	if (TR_DidHit(INVALID_HANDLE))
	{
		TR_GetEndPosition(end, INVALID_HANDLE);
		array = end;
	}
}

public bool:GEP_TEF(entity, mask, any:data)
{
	if (entity == data)
	{
		return false;
	}
	return true;
}